package models

import (
	"time"

	"gorm.io/gorm"
)

// TunnelMapping 隧道映射数据库模型
type TunnelMapping struct {
	ID            uint           `json:"id" gorm:"primarykey"`
	TunnelID      string         `json:"tunnel_id" gorm:"uniqueIndex;not null;size:100" binding:"required"`
	UserID        uint           `json:"user_id" gorm:"not null;index" binding:"required"`
	ServiceName   string         `json:"service_name" gorm:"not null;size:200" binding:"required"`
	LocalIP       string         `json:"local_ip" gorm:"not null;size:50" binding:"required"`
	LocalPort     int64          `json:"local_port" gorm:"not null" binding:"required"`
	PublicPort    int64          `json:"public_port" gorm:"not null;index" binding:"required"`
	CustomDomain  *string        `json:"custom_domain,omitempty" gorm:"size:255;index"`
	DNSProvider   *string        `json:"dns_provider,omitempty" gorm:"size:50"`
	DNSRecordID   *string        `json:"dns_record_id,omitempty" gorm:"size:100"`
	TunnelType    string         `json:"tunnel_type" gorm:"not null;default:http;size:20" binding:"required"`
	Status        string         `json:"status" gorm:"not null;default:active;size:20;index" binding:"required"`
	PublicURL     *string        `json:"public_url,omitempty" gorm:"size:500"`
	ErrorMessage  *string        `json:"error_message,omitempty" gorm:"type:text"`
	TCPTunnelPort int64          `json:"tcp_tunnel_port" gorm:"not null;default:0;index"`
	CreatedAt     time.Time      `json:"created_at"`
	UpdatedAt     time.Time      `json:"updated_at"`
	DeletedAt     gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联关系
	User User `json:"user,omitempty" gorm:"foreignKey:UserID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}

// TableName 指定表名
func (TunnelMapping) TableName() string {
	return "tb_tunnel_mapping"
}

// TunnelMappingRequest 创建/更新隧道映射的请求参数
type TunnelMappingRequest struct {
	ServiceName   string  `json:"service_name" binding:"required"`
	LocalIP       string  `json:"local_ip" binding:"required"`
	LocalPort     int64   `json:"local_port" binding:"required"`
	CustomDomain  *string `json:"custom_domain,omitempty"`
	DNSProvider   *string `json:"dns_provider,omitempty"`
	TunnelType    string  `json:"tunnel_type" binding:"required"`
	TCPTunnelPort int64   `json:"tcp_tunnel_port,omitempty"`
}

// TunnelMappingResponse 隧道映射响应参数
type TunnelMappingResponse struct {
	ID            uint    `json:"id"`
	TunnelID      string  `json:"tunnel_id"`
	ServiceName   string  `json:"service_name"`
	LocalIP       string  `json:"local_ip"`
	LocalPort     int64   `json:"local_port"`
	PublicPort    int64   `json:"public_port"`
	CustomDomain  *string `json:"custom_domain,omitempty"`
	DNSProvider   *string `json:"dns_provider,omitempty"`
	DNSRecordID   *string `json:"dns_record_id,omitempty"`
	TunnelType    string  `json:"tunnel_type"`
	Status        string  `json:"status"`
	PublicURL     *string `json:"public_url,omitempty"`
	ErrorMessage  *string `json:"error_message,omitempty"`
	TCPTunnelPort int64   `json:"tcp_tunnel_port"`
	CreatedAt     string  `json:"created_at"`
	UpdatedAt     string  `json:"updated_at"`
}

// ToResponse 转换为响应格式
func (tm *TunnelMapping) ToResponse() *TunnelMappingResponse {
	return &TunnelMappingResponse{
		ID:            tm.ID,
		TunnelID:      tm.TunnelID,
		ServiceName:   tm.ServiceName,
		LocalIP:       tm.LocalIP,
		LocalPort:     tm.LocalPort,
		PublicPort:    tm.PublicPort,
		CustomDomain:  tm.CustomDomain,
		DNSProvider:   tm.DNSProvider,
		DNSRecordID:   tm.DNSRecordID,
		TunnelType:    tm.TunnelType,
		Status:        tm.Status,
		PublicURL:     tm.PublicURL,
		ErrorMessage:  tm.ErrorMessage,
		TCPTunnelPort: tm.TCPTunnelPort,
		CreatedAt:     tm.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:     tm.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
}

// TunnelMappingListResponse 隧道映射列表响应
type TunnelMappingListResponse struct {
	Tunnels []TunnelMappingResponse `json:"tunnels"`
	Count   int                     `json:"count"`
	Page    int                     `json:"page,omitempty"`
	Size    int                     `json:"size,omitempty"`
	Total   int64                   `json:"total,omitempty"`
}

// TunnelStatus 隧道状态常量
const (
	TunnelStatusActive       = "active"
	TunnelStatusInactive     = "inactive"
	TunnelStatusConnecting   = "connecting"
	TunnelStatusDisconnected = "disconnected"
	TunnelStatusError        = "error"
)

// TunnelType 隧道类型常量
const (
	TunnelTypeHTTP = "http"
	TunnelTypeTCP  = "tcp"
	TunnelTypeUDP  = "udp"
)
